const http = require('http');
const fs = require('fs');
const querystring = require('querystring');

exports.render = function (ctx, filename, data) {

    var path = this.templateDir;

    return new Promise((resolve) => {

        var html = fs.readFileSync(path + '/' + filename, "utf-8");  

        //发送 http Post 请求  
        var postData = querystring.stringify({
            html: html,
            data: JSON.stringify(data)
        });

        var options = {
            hostname: '47.90.3.153',
            port: 7011,
            path: '/build',
            method: 'POST',
            headers: {
                "Accept": "application/json, text/javascript, */*; q=0.01",
                "Accept-Encoding": "gzip, deflate",
                "Accept-Language": "zh-CN,zh;q=0.8",
                "Connection": "keep-alive",
                "Content-Length": postData.length,
                "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                "Cookie": "imooc_uuid=6cc9e8d5-424a-4861-9f7d-9cbcfbe4c6ae; imooc_isnew_ct=1460873157; loginstate=1; apsid=IzZDJiMGU0OTMyNTE0ZGFhZDAzZDNhZTAyZDg2ZmQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMjkyOTk0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGNmNmFhMmVhMTYwNzRmMjczNjdmZWUyNDg1ZTZkMGM1BwhXVwcIV1c%3DMD; PHPSESSID=thh4bfrl1t7qre9tr56m32tbv0; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1467635471,1467653719,1467654690,1467654957; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1467655022; imooc_isnew=2; cvde=577a9e57ce250-34",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2763.0 Safari/537.36",
                "X-Requested-With": "XMLHttpRequest",
            }
        }


        var body = '';
        var req = http.request(options, function (res) {
            console.log('Status:', res.statusCode);
            console.log('headers:', JSON.stringify(res.headers));
            res.setEncoding('utf-8');
            res.on('data', function (chun) {
                body = chun;
            });
            res.on('end', function () {
                resolve(body);
            });
        });

        req.on('error', function (err) {
            console.log(err);
        });

        req.write(postData);
        req.end();
    }).then((html) => {
        ctx.body = html;
    });;
};